import type { TableColumn } from '@/types'
import { uuid } from '@/utils'
import { loadMeteringDeviceListByPage, delBindMeteringDevice } from '@/api/modules/gateway'

export interface MeasuringTool {
  /** 计量器具编号 */
  measuringToolNumber: string
  /** 计量器具名称 */
  measuringToolName: string
  /** 能源品种 */
  energyVariety: string
  /** 规格型号 */
  measuringToolModel: string
  /** 安装位置 */
  measuringToolPosition: string

  [key: string]: any
}

const seed = 10000
async function getData(p: any) {
  const { pageNumber, pageSize } = p
  return new Promise<any>((resolve) => {
    setTimeout(() => {
      resolve({
        records: Array.from({ length: pageSize }, (_, i) => ({
          id: seed + pageNumber * pageSize + i,
          measuringToolNumber: `编号${seed + pageNumber * pageSize + i}`,
          measuringToolName: `测试${uuid()}`,
          energyVariety: '水',
          measuringToolModel: '123',
          measuringToolPosition: '1',
        })),
        total: 100,
      })
    }, 500)
  })
}
export function useTableData() {
  const columns = shallowRef<TableColumn<MeasuringTool>[]>([
    { key: uuid(), type: 'selection' },
    { key: uuid(), label: '计量器具编号', prop: 'measuringToolNumber' },
    { key: uuid(), label: '计量器具名称', prop: 'measuringToolName' },
    { key: uuid(), label: '能源品种', prop: 'energyVariety' },
    { key: uuid(), label: '规格型号', prop: 'measuringToolModel' },
    { key: uuid(), label: '安装位置', prop: 'measuringToolPosition' },
  ])
  const [tableData, setTableData] = useShallowState<MeasuringTool[]>([])
  const pagination = reactive({
    pageNumber: 1,
    pageSize: 5,
    total: 0,
  })
  const [loading, setLoading] = useState(false)

  async function confirmDel() {
    return ElMessageBox.confirm(
      '确认要移除该关联数据吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
  }

  async function getLists(type: 0 | 1, params: any) {
    setLoading(true)
    try {
      const { data } = await loadMeteringDeviceListByPage(
        params.pageNumber,
        params.pageSize,
        type === 1 ? params.pid : undefined,
        params.pointId,
        params.keyword,
      )
      // 字段映射关系
      // {
      //   id: id
      //   measuringToolNumber: code,
      //   measuringToolName: name,
      //   energyVariety: energyName,
      //   measuringToolModel: specification,
      //   measuringToolPosition: installationLocation
      // }
      const mapFn: (v: any) => MeasuringTool = (v: any) => ({
        id: v.id,
        measuringToolNumber: v.code,
        measuringToolName: v.name,
        energyVariety: v.energyName,
        measuringToolModel: v.specification,
        measuringToolPosition: v.installationLocation,
      })
      setTableData(
        (data.records || []).map(mapFn),
      )
      pagination.total = data.total
    }
    catch (e) {}
    finally {
      setLoading(false)
    }
  }

  async function del(id: string | number) {
    const res = await confirmDel().catch(() => {})
    if (res !== 'confirm')
      return false
    try {
      await delBindMeteringDevice(id as number)
      ElMessage.success('移除成功')
      return true
    }
    catch (e) {}
  }

  return {
    columns,
    loading,
    tableData,
    pagination,
    getLists,
    del,
  }
}
